package com.jymj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jymj.entity.KeyValueEntity;
import com.jymj.entity.SysUserEntity;
import com.jymj.entity.YlEntity;
import com.jymj.entity.dto.ApplyDto;
import com.jymj.entity.huji.ZhaiEntity;
import com.jymj.entity.model.PointAndTypeEntity;
import com.jymj.entity.zjd.ApplyCountEntity;
import com.jymj.entity.zjd.ApplyEntity;
import com.jymj.entity.zjd.ApplyLandEntity;
import com.jymj.entity.zjd.ApplyLogEntity;
import com.jymj.enumEntity.*;
import com.jymj.service.*;
import com.jymj.utils.*;
import com.jymj.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by ljj on 2018/9/12.
 */
@RestController
@RequestMapping("/apply")
@Transactional(rollbackFor = Exception.class)
public class ApplyController extends AbstractController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private ApplyLogService applyLogService;
    @Autowired
    private YlService ylService;
    @Autowired
    private ZhaiService zhaiService;


    /**
     * 查询备案 待审核2  通过3 驳回5 施工7列表
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody ApplyDto applyDto){
        PageHelper.startPage(applyDto.getPage(),applyDto.getLimit());
        List<ApplyEntity> applyEntities = applyService.queryList(applyDto);
        PageInfo<ApplyEntity> applyEntityPageInfo = new PageInfo<>(applyEntities);
        return R.ok().put("data", applyEntityPageInfo);
    }


    /**
     * 翻建列表数量统计
     * @param applyDto
     * @return
     */
    @PostMapping("/queryStatistical")
    public R queryStatistical(@RequestBody ApplyDto applyDto){
        KeyValueEntity keyValueEntity = applyService.queryStatistical(applyDto);
        return R.ok().put("data",keyValueEntity);
    }

    /**
     * 点查询
     */
    @PostMapping("/applyByPoint")
    public R list1(@RequestBody ApplyDto applyDto){
        SysUserEntity user = getUser();
        List<ApplyEntity> applyEntities =  new ArrayList<>();
        YlEntity ylEntity = ylService.getYLbyPoint(applyDto.getPoint(),user.getCode());
        if (ylEntity == null){
            return R.error("无院落信息");
        }

        if(ylEntity!=null && ylEntity.getObjectid()!=null){
             applyEntities = applyService.queryObjectByylId(ylEntity.getObjectid());
            if (applyEntities.size() > 0){
                for (ApplyEntity applyEntity : applyEntities) {
                    applyEntity.setZhaiEntities(
                            zhaiService.getZhaiEntity(ylEntity.getObjectid(),applyEntity.getHousehold()));
                    applyEntity.setZhaiEntity(
                            zhaiService.getzhai(ylEntity.getObjectid(),applyEntity.getHousehold()));
                    applyEntity.setYlEntity(ylEntity);
                }
            }else {
                ApplyEntity applyEntity = new ApplyEntity();
                applyEntity.setYlEntity(ylEntity);
                applyEntity.setZhaiEntities(
                        zhaiService.getZhaiEntity(ylEntity.getObjectid(),null));
                applyEntity.setZhaiEntity(zhaiService.getzhai(ylEntity.getObjectid(),null));
                applyEntities.add(applyEntity);
            }
        }
        return R.ok().put("data",applyEntities);
    }

    /**
     * 查询详情
     * @return
     */
    @PostMapping("/info")
    public R info(@RequestBody Map map){
        Integer applyId = map.get("applyId") == null ? null : Integer.valueOf(map.get("applyId").toString());
        if (applyId == null){
            return R.error("无参数");
        }
        ApplyEntity applyEntity =  applyService.queryObject(applyId);
        return R.ok().put("data",applyEntity);
    }

    /**
     *添加申请
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody ApplyEntity apply){
        //校验类型
        ValidatorUtils.validateEntity(apply);
        applyService.save(apply);
        return R.ok().put("data",apply.getId());
    }

    /**
     *修改申请
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody ApplyEntity apply){
        //校验类型
        ValidatorUtils.validateEntity(apply);
        applyService.update(apply);
        return R.ok();
    }

    /**
     * 翻建汇总  按镇
     * @param applyDto
     * @return
     */
    @PostMapping("/getSummary")
    public R getSummary(@RequestBody ApplyDto applyDto){
        List<KeyValueEntity> keyValueEntities = applyService.getSummary(applyDto);
        return R.ok().put("data",keyValueEntities);
    }

    /**
     * 删除户籍信息
     * @param ids
     * @return
     */
    @PostMapping("/deleteHuji")
    public R deleteHuji(@RequestBody Long... ids){
        zhaiService.deleteHuji(ids);
        return R.ok("删除成功");
    }

    /**
     * 删除翻建房屋信息
     * @param ids
     * @return
     */
    @PostMapping("/deleteFjfw")
    public R deleteFjfw(@RequestBody Long... ids){
        applyService.deleteFjfw(ids);
        return R.ok("删除成功");
    }

    /**
     * 删除（备案状态）
     */
    @PostMapping("/delete")
    public R dele(@RequestBody Integer... ylIds){
        //修改yl属性
        ylService.updateYlBatch(ylIds);
        //删除申请表
        applyService.deleteBatch(ylIds);
        return R.ok();
    }

    /**
     * 聚合展示
     * @param pointAndTypeEntity
     * @return
     */
    @PostMapping("/queryCount")
    public R queryCount(@RequestBody PointAndTypeEntity pointAndTypeEntity){
        List<ApplyCountEntity> applyCountEntities = new ArrayList<>();
        if (pointAndTypeEntity.getType() != null && pointAndTypeEntity.getType() == 1){
            applyCountEntities = applyService.queryCount1(pointAndTypeEntity);
        }else if (pointAndTypeEntity.getType() != null && pointAndTypeEntity.getType() == 2){
            applyCountEntities = applyService.queryCount2(pointAndTypeEntity);
        }else if (pointAndTypeEntity.getType() != null && pointAndTypeEntity.getType() == 3){
            applyCountEntities = applyService.queryCount3(pointAndTypeEntity);
        }else {
            applyCountEntities = applyService.queryCount4(pointAndTypeEntity);
        }
        return R.ok().put("data",applyCountEntities);
    }

    /**
     * 获取所有枚举
     * @return
     */
    @PostMapping("/getAllEnum")
    public R getAllEnum(){
        HashMap<String, Object> enummap = new HashMap<>();
        //遍历所有枚举
        //民族
        List<Map<String, Object>> listNationEnum = new ArrayList<>();
        int i = 0;
        while (EnumUtil.getByindex(i, NationEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, NationEnum.class).getName());
            listNationEnum.add( map);
            i++;
        }
        //性别
        List<Map<String, Object>> listSexEnum = new ArrayList<>();
        i = 0;
        while (EnumUtil.getByindex(i, SexEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, SexEnum.class).getName());
            listSexEnum.add( map);
            i++;
        }
        //户别
        List<Map<String, Object>> listHuEnum = new ArrayList<>();
        i = 0;
        while (EnumUtil.getByindex(i, HuTypeEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, HuTypeEnum.class).getName());
            listHuEnum.add( map);
            i++;
        }
        //社会关系
        List<Map<String, Object>> listSociarateEnum = new ArrayList<>();
        i = 0;
        while (EnumUtil.getByindex(i, SociarateEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, SociarateEnum.class).getName());
            listSociarateEnum.add( map);
            i++;
        }
        //地类
        List<Map<String, Object>> listLandTypeEnum= new ArrayList<>();
        i = 0;
        while (EnumUtil.getByindex(i, LandTypeEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, LandTypeEnum.class).getName());
            listLandTypeEnum.add( map);
            i++;
        }
        //建房类型
        List<Map<String, Object>> listHouseTypeEnum = new ArrayList<>();
        i = 0;
        while (EnumUtil.getByindex(i, HouseTypeEnum.class)!=null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("index", i);
            map.put("name", EnumUtil.getByindex(i, HouseTypeEnum.class).getName());
            listHouseTypeEnum.add(map);
            i++;
        }
        //放入map
        enummap.put("NationEnum", listNationEnum);
        enummap.put("SexEnum", listSexEnum);
        enummap.put("HuEnum", listHuEnum);
        enummap.put("SociarateEnum", listSociarateEnum);
        enummap.put("LandTypeEnum", listLandTypeEnum);
        enummap.put("HouseTypeEnum", listHouseTypeEnum);
        return R.ok().put("data",enummap);

    }

    /**
     *添加申请驳回日志
     * @return
     */
    @RequestMapping("/saveLog")
    public R saveLog(@RequestBody ApplyLogEntity applyLog){
        //校验类型
        ValidatorUtils.validateEntity(applyLog);
        applyLog.setBhdate(new Date());
        applyLogService.save(applyLog);
        return R.ok();
    }


}
